//
//  LDBaseSetTitleVCViewController.swift
//  DingDongZP
//
//  Created by Zhongnan on 16/5/8.
//  Copyright © 2016年 iyhjiang.com. All rights reserved.
//

import UIKit
import WSProgressHUD

enum LDBaseSetTitleVCTextLimit:Int {
    case Mini = 15
    case Medium = 140
}

<<<<<<< HEAD
class LDBaseSetTitleVCViewController: UIViewController {
=======
class LDBaseSetTitleVCViewController: LDBaseCustomizeVC {
>>>>>>> dingdong2.1.0
    
    var textStr = ""
    var textField:UITextField?
    var textView:UITextView?
    var countLab:UILabel?
    var limit:LDBaseSetTitleVCTextLimit = .Mini
    
    var sendColsure:((title:String)->())?
    var isChange = false
    
    override func viewDidLoad() {
        super.viewDidLoad()
        setDefaultContent()
        setNav()
    }
    
    func setNav() {
        
        
<<<<<<< HEAD
        let nav = navigationController as? LDRootNavVC
=======
        let nav = navigationController as? LDRTRootNavVC
>>>>>>> dingdong2.1.0
        nav?.tapBackNavClosure({ [weak self] in
            
            if self?.isChange == true {
                
                self?.showLDAlertView("信息已编辑,是否退出", sure: {
                    
                    self?.navigationController?.popViewControllerAnimated(true)
<<<<<<< HEAD
                    }, cancel: nil)
            }
            
            })
=======
                    
                    }, cancel: nil)
            }
            
        })
>>>>>>> dingdong2.1.0
        
        let rightBar = UIBarButtonItem.init(image: UIImage.init(named: "common_nav_ok"), style: UIBarButtonItemStyle.Done, target: self, action: #selector(sendAdvantageInfo))
        navigationItem.rightBarButtonItem = rightBar
    }
    
    func sendAdvantageInfo() {
        
        textField?.endEditing(true)
        textView?.endEditing(true)
        
        if limit == .Mini {
            
            if textField?.text?.characters.count < 1 {
                
                showLDAlertView("内容不能为空")
            } else {
                sendColsure!(title: (textField?.text)!)
            }
        } else {
            
            
            if textView?.text?.characters.count < 1 {
                
                showLDAlertView("内容不能为空")
            } else {
                
                sendColsure!(title: (textView?.text)!)
            }
        }
    }
    
    //确认
    func getTitleFromColsure(closue:((titleStr:String)->Void)) {
        sendColsure = closue
    }
<<<<<<< HEAD
=======
    //
>>>>>>> dingdong2.1.0
}
extension LDBaseSetTitleVCViewController:UITextViewDelegate {
    
    func setDefaultContent() {
        view.backgroundColor = LDGolbalColor.BgGoundGray
        
        textField = UITextField.init()
        textView = UITextView.init()
        
        if limit == .Mini {
            
            textView?.hidden = true
            textField?.hidden = false
            
            textField?.delegate = self
            textField?.backgroundColor = LDGolbalColor.White
            textField?.font = LDFont.Font15
            
        } else if limit == .Medium {

            textField?.hidden = true
            textView?.hidden = false
            
            textView?.delegate = self
            textView?.backgroundColor = LDGolbalColor.White
            textView?.font = LDFont.Font15
        }

        
        
        countLab = UILabel.setDetailsLab("0/\(limit.rawValue)", fontSize: 14, titleColor: LDGolbalColor.GreenAssist, Alignment: .Right)
<<<<<<< HEAD
        if textStr.characters.count != 0 {
=======
        
        if textStr.characters.count != 0 {
            
>>>>>>> dingdong2.1.0
            textField?.text = textStr
            textView?.text = textStr
            countLab?.text = "\(String.CountOfWordFromString(textStr))/\(limit.rawValue)"
        }
        
        view.addSubview(textField!)
        view.addSubview(textView!)
        view.addSubview(countLab!)
        
<<<<<<< HEAD
=======
        textField?.rac_textSignal().subscribeNext({ [weak self] (text) in
            
            let textStr = text as? String ?? ""
            let textLength = textStr.characters.count ?? 0//String.CountOfWordFromString(textStr!)
            
            if textLength > self?.limit.rawValue {
                
                let str = NSString.init(string: textStr)
                let range = NSRange.init(location: 0, length: (self?.limit.rawValue)!)
                
                let rangstr = str.substringWithRange(range)
                self?.textField!.text = rangstr
                
            }
            
            self?.countLab?.text = "\(textLength)/\(self?.limit.rawValue ?? 0)"

            if textLength == (self?.limit.rawValue) {
                
                self?.showLDAlertView("字数超过\(self?.limit.rawValue ?? 0)")
            } else {
                
                self?.isChange = true
            }
            
        })
        
>>>>>>> dingdong2.1.0
        if limit == .Mini {
            
            textField?.snp_makeConstraints(closure: { (make) in
                make.top.equalTo(LDMargin_H*2)
                make.right.left.equalTo(0)
                make.height.equalTo(45)
            })
            countLab?.snp_makeConstraints(closure: { (make) in
                make.top.equalTo(textField!.snp_bottom).offset(5)
                make.right.equalTo(-LDMargin_H*2)
                make.height.equalTo(15)
            })
            
        } else if limit == .Medium {
            
            textView?.snp_makeConstraints(closure: { (make) in
                make.top.equalTo(LDMargin_H*2)
                make.left.equalTo(0)
                make.width.equalTo(LDFrame.ScreenWidth)
                make.height.equalTo(240)
            })
            countLab?.snp_makeConstraints(closure: { (make) in
                make.top.equalTo(textView!.snp_bottom).offset(5)
                make.right.equalTo(-LDMargin_H*2)
                make.height.equalTo(15)
            })
        }
    }
    
    func textView(textView: UITextView, shouldChangeTextInRange range: NSRange, replacementText text: String) -> Bool {
        
        let textLength = String.CountOfWordFromString(textView.text)
        countLab?.text = "\(textLength)/\(limit.rawValue)"
        
        let textStr = textView.text

        if textLength > limit.rawValue {
            let str = NSString.init(string: textStr!)
            let range = NSRange.init(location: 0, length: limit.rawValue)
            
            let rangstr = str.substringWithRange(range)
            textView.text = rangstr
            
        }
        
        if textLength >= limit.rawValue  && text.characters.count != 0{
            showLDAlertView("字数超过\(limit.rawValue)")
            return false
        } else {
            isChange = true
            return true
        }
    }
    
    override func touchesBegan(touches: Set<UITouch>, withEvent event: UIEvent?) {
        textField?.endEditing(true)
    }
}
extension LDBaseSetTitleVCViewController:UITextFieldDelegate{
    
<<<<<<< HEAD
    func textField(textField: UITextField, shouldChangeCharactersInRange range: NSRange, replacementString string: String) -> Bool {
        
        let textStr = textField.text
        let textLength = String.CountOfWordFromString(textStr!)
        
        if textLength > limit.rawValue {
            let str = NSString.init(string: textStr!)
            let range = NSRange.init(location: 0, length: limit.rawValue)
            
            let rangstr = str.substringWithRange(range)
            textField.text = rangstr
            
        }
        
        countLab?.text = "\(textLength)/\(limit.rawValue)"
        if textLength > (limit.rawValue - 1) && string.characters.count != 0{
            showLDAlertView("字数超过\(limit.rawValue)")
            return false
        } else {
            isChange = true
            return true
        }
    }
=======
    
//    func textField(textField: UITextField, shouldChangeCharactersInRange range: NSRange, replacementString string: String) -> Bool {
//        
//        let textStr = textField.text
//        let textLength = textStr?.characters.count ?? 0//String.CountOfWordFromString(textStr!)
//        
//        if textLength > limit.rawValue {
//            
//            let str = NSString.init(string: textStr!)
//            let range = NSRange.init(location: 0, length: limit.rawValue)
//            
//            let rangstr = str.substringWithRange(range)
//            textField.text = rangstr
//            
//        }
//        
//        countLab?.text = "\(textLength)/\(limit.rawValue)"
//        
//        if textLength > (limit.rawValue - 1) && string.characters.count != 0 {
//            
//            showLDAlertView("字数超过\(limit.rawValue)")
//            return false
//        } else {
//            
//            isChange = true
//            return true
//        }
//    }
>>>>>>> dingdong2.1.0
    
    func textFieldDidBeginEditing(textField: UITextField) {
        
    }
}